//
//  HomeTableViewController.swift
//  Weibo
//
//  Created by 李振兴 on 15/10/14.
//  Copyright © 2015年 李振兴. All rights reserved.
//

import UIKit
/// 原创微博的可重用 ID
let StatusCellNormalId = "StatusCellNormalId"
/// 转发微博的可重用 ID
let StatusCellRetweetedId = "StatusCellRetweetedId"

class HomeTableViewController: VisitorTableViewController {
    private lazy var statusListViewModel = StatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        print(visitorView)
        if !UserAccountViewModel.sharedUserAccount.isLogon {
            visitorView?.setupInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        prepareTableView()
        loadData()
    }
    /// 准备表格
    private func prepareTableView() {
        // 注册可重用 cell
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: StatusCellRetweetedId)
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusCellNormalId)
        //tableView.rowHeight = 200
         //自动计算行高 - 需要一个自上而下的自动布局的控件，指定一个向下的约束
        tableView.estimatedRowHeight = 200
        //tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
        refreshControl = WBRefreshControl()
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        tableView.tableFooterView = pullupView
    }
    ///加载数据
    @objc private func loadData(){
        refreshControl?.beginRefreshing()
        statusListViewModel.loadStatus(isPullup: pullupView.isAnimating()) { (isSuccess) -> () in
            //刷新结束
            self.refreshControl?.endRefreshing()
            //上拉动画结束
            self.pullupView.stopAnimating()
            if !isSuccess {
                print("加载statusList失败")
            }else{
               
                self.tableView.reloadData()
            }
        }
    }
    // MARK: - 懒加载控件
    /// 上拉刷新提示视图
    private lazy var pullupView: UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicator.color = UIColor.lightGrayColor()
        
        return indicator
        }()

}
///数据源方法
extension HomeTableViewController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let vm = statusListViewModel.statusList[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(vm.cellId, forIndexPath: indexPath) as! StatusCell
        cell.viewModel = vm
        if indexPath.row == statusListViewModel.statusList.count - 1 && !pullupView.isAnimating(){
            pullupView.startAnimating()
            loadData()
        }
        
        return cell
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return statusListViewModel.statusList[indexPath.row].rowHeight
        
  }
}